當你的程式碼庫超過數百行時,它將從一個簡單的腳本轉變為一個 系統。為了避免認知過載,Rust 使用層次化的 模組系統 來將功能劃分為邏輯清晰、易於管理的範疇。
1. 可擴展性的重要性
在一個龐大的系統中,你不應需要把整個架構都記在腦中。模組能讓你隔離實作細節,僅透過公開介面暴露必要的內容。
2. 雙套件架構
一個單一的 Rust 套件 作為容器。它可以同時包含一個函式庫套件(src/lib.rs)用於核心邏輯,以及一個可執行檔套件(src/main.rs)作為可執行檔的入口點。這確保了「系統做什麼」與「使用者如何互動」之間有明確的區隔。 系統做什麼 與 使用者如何互動 之間有明確的區隔。
3. 組織基礎
透過使用 cargo new --lib,你便優先考量模組化。在餐廳管理系統中,「前廳」(接待)與「後廚」(烹飪)被分離,允許多個前端(命令列、網頁、行動裝置)共享相同的基礎函式庫邏輯。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
By default, what is the visibility of items (functions, modules) in Rust?
Public to the entire package.
Private to the parent module.
Public within the crate root only.
Accessible via 'super' only.
✅ Correct!
Rust follows the principle of encapsulation: everything is private by default unless explicitly marked with 'pub'.❌ Incorrect
Rust defaults to privacy to protect implementation details from accidental outside dependency.QUESTION 2
Which file is considered a 'Crate Root' by convention for a library?
src/main.rs
$src/mod.rs$
$src/lib.rs$
Cargo.toml
✅ Correct!
src/lib.rs is the standard entry point for library crates.❌ Incorrect
src/main.rs is for binaries; Cargo.toml is the manifest, not a source file.QUESTION 3
Can a single Rust package contain both a library and a binary crate?
No, it must be one or the other.
Yes, by having both src/lib.rs and src/main.rs.
Yes, but they must share the same file name.
Only if defined in separate Cargo workspaces.
✅ Correct!
This is a common pattern: the library contains logic, and the binary provides the interface.❌ Incorrect
Rust packages are designed to support multiple crates of different types (lib vs bin).QUESTION 4
What is the purpose of the 'crate' keyword in a path?
To access an external dependency.
To start an absolute path from the crate root.
To define a new module.
To import a library from Crates.io.
✅ Correct!
Using 'crate::' allows you to navigate the module tree from the very top.❌ Incorrect
External dependencies are handled by their names; 'crate' refers to the current crate's root.QUESTION 5
Which command initializes a new project focused on reusable library logic?
cargo new project_name
cargo build --lib
cargo new project_name --lib
cargo init --bin
✅ Correct!
The --lib flag tells Cargo to create a src/lib.rs instead of a src/main.rs.❌ Incorrect
'cargo new' without flags defaults to a binary project.Architectural Design: Restaurant System
Applying module theory to a scaling codebase.
You are designing a restaurant management library. You have a module 'back_of_house' that handles 'cooking'. You need to call 'cook_order' from an external binary that imports your library.
Q
1. If 'back_of_house' is a module in src/lib.rs, how must 'cook_order' be declared to be reachable by the binary?
Solution:
Both the module 'back_of_house' and the function 'cook_order' must be marked with the 'pub' keyword. Marking a parent public does not automatically make children public.
Both the module 'back_of_house' and the function 'cook_order' must be marked with the 'pub' keyword. Marking a parent public does not automatically make children public.
Q
2. Why is it beneficial to put the cooking logic in a library crate instead of directly in the main binary?
Solution:
It promotes reusability. By putting logic in src/lib.rs, it can be shared by multiple binaries (like a kitchen display app and a mobile ordering app) and allows for easier unit testing.
It promotes reusability. By putting logic in src/lib.rs, it can be shared by multiple binaries (like a kitchen display app and a mobile ordering app) and allows for easier unit testing.